/**
 * 
 */
package repository.relsys;






import java.io.FileInputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedList;


import java.util.Map;

import org.apache.log4j.Logger;


import com.hp.hpl.jena.rdf.model.Model;
import com.hp.hpl.jena.rdf.model.ModelFactory;
import com.hp.hpl.jena.util.FileManager;

import repository.relsys.app.Settings;
import repository.relsys.config.Property;
import repository.relsys.domainmodel.ltool.interfaces.Blog;
import repository.relsys.domainmodel.ltool.interfaces.CollaborativeTagging;
import repository.relsys.domainmodel.ltool.interfaces.ContactList;
import repository.relsys.domainmodel.ltool.interfaces.Feature;
import repository.relsys.domainmodel.ltool.interfaces.GeneralInformation;
import repository.relsys.domainmodel.ltool.interfaces.InstantMessaging;
import repository.relsys.domainmodel.ltool.interfaces.LearningTool;
import repository.relsys.domainmodel.ltool.interfaces.MultimediaHosting;
import repository.relsys.domainmodel.ltool.interfaces.SiteSearch;
import repository.relsys.domainmodel.ltool.interfaces.SocialNetworking;
import repository.relsys.domainmodel.ltool.interfaces.TechnicalInformation;
import repository.relsys.domainmodel.ltool.interfaces.UserProfileManagement;
import repository.relsys.domainmodel.ltool.pojos.LearningToolCls;
import repository.relsys.domainmodel.ltool.pojos.MobileDeviceCls;
import repository.relsys.domainmodel.ltool.pojos.OperetingSystemCls;
import repository.relsys.domainmodel.ltool.pojos.ToolAvailabilityCls;
import repository.relsys.domainmodel.ltool.pojos.ToolCostCls;

import repository.relsys.services.DataModelManager;

import repository.relsys.services.RelsysReadWrite;
import repository.relsys.services.datacentral.FileDataProvider;
import repository.relsys.services.spring.RelsysResourceFactory;
import repository.relsys.util.json.JSONExporter;
import repository.relsys.util.json.JSONExporterCls;
import thewebsemantic.RDF2Bean;

public class RelsysTest2 {

	private static Logger logger = Logger
			.getLogger(RelsysTest2.class.getName());

	public static void main(String[] args) {
		try {
			

			Settings.getInstance();


			RelsysTest2.initRepository();
			logger.info("Repository initialised!");
			
			System.in.read();

		
			DataModelManager.getInstance().flush();

		} catch (Exception e) {
			logger.error(e.getMessage());
		}
	}

	public static void initRepository() throws Exception {

		
		RelsysResourceFactory rf = new RelsysResourceFactory();
		//Collection<LearningTool> ltools = new LinkedList<LearningTool>();
		RelsysReadWrite rrw = new RelsysReadWrite();
       
		/*LearningTool ltool1 = (LearningTool) rf
				.createResource(LearningTool.class);
		ltool1.setTitle("WordPress");

		GeneralInformation gi1 = (GeneralInformation) rf
				.createResource(GeneralInformation.class);

		gi1.setDescription("WordPress is ...");
		gi1.setToolURL("http://www.wordpress.com");
		gi1.setToolCost(ToolCostCls.FREE);
		gi1.setToolAvailability(ToolAvailabilityCls.HOSTED);

		ltool1.setGeneralInfo(gi1);

		TechnicalInformation ti1 = (TechnicalInformation) rf
				.createResource(TechnicalInformation.class);
		ti1.setLaunchedTime("12-01-2001");
		ti1.setVersion("2.9");
		ti1.registration(true);
		ti1.isOpenSource(false);
		ti1.addMD(MobileDeviceCls.BLACKBERRY);
		ti1.addMD(MobileDeviceCls.ANDROID);
		ti1.addMD(MobileDeviceCls.IPHONE);
		ti1.addOS(OperetingSystemCls.LINUX);
		ti1.addOS(OperetingSystemCls.WINDOWS);
		ti1.addOS(OperetingSystemCls.MAC);

		ltool1.setTechnicalInfo(ti1);

		Collection<Feature> features1 = new LinkedList<Feature>();
		Blog blog = (Blog) rf.createResource(Blog.class);
		ContactList folowers = (ContactList) rf
				.createResource(ContactList.class);
		features1.add(blog);
		features1.add(folowers);

		ltool1.setFeatures(features1);

		LearningTool ltool2 = (LearningTool)rf.createResource(LearningTool.class);
		ltool2.setTitle("Facebook");
		ltool2.setToolImgURL("http://upload.wikimedia.org/wikipedia/commons/thumb/0/06/Facebook.svg/230px-Facebook.svg.png");

		GeneralInformation gi2 = (GeneralInformation)rf.createResource(GeneralInformation.class);

		gi2.setDescription("Facebook is ...");
		gi2.setToolURL("http://www.facebook.com");
		gi2.setToolCost(ToolCostCls.FREE);
		gi2.setToolAvailability(ToolAvailabilityCls.HOSTED);

		ltool2.setGeneralInfo(gi2);

		TechnicalInformation ti2 = (TechnicalInformation) rf.createResource(TechnicalInformation.class);
		ti2.setLaunchedTime("12-01-2001");
		ti2.setVersion("2.9");
		ti2.registration(true);
		ti2.isOpenSource(false);
		ti2.addMD(MobileDeviceCls.BLACKBERRY);
		ti2.addMD(MobileDeviceCls.ANDROID);
		ti2.addMD(MobileDeviceCls.IPHONE);
		ti2.addOS(OperetingSystemCls.LINUX);
		ti2.addOS(OperetingSystemCls.WINDOWS);
		ti2.addOS(OperetingSystemCls.MAC);

		ltool2.setTechnicalInfo(ti2);

		Collection<Feature> facefeatures = new LinkedList<Feature>();
		UserProfileManagement upm = (UserProfileManagement) rf.createResource(UserProfileManagement.class);
		SocialNetworking sn = (SocialNetworking) rf.createResource(SocialNetworking.class);
		SiteSearch ss = (SiteSearch) rf.createResource(SiteSearch.class);
		MultimediaHosting mh = (MultimediaHosting) rf.createResource(MultimediaHosting.class);
		InstantMessaging im = (InstantMessaging) rf.createResource(InstantMessaging.class);
		CollaborativeTagging ct = (CollaborativeTagging) rf.createResource(CollaborativeTagging.class);

		facefeatures.add(upm);
		facefeatures.add(sn);
		facefeatures.add(ss);
		facefeatures.add(mh);
		facefeatures.add(im);
		facefeatures.add(ct);
		ltool2.setFeatures(facefeatures);

		ltools.add(ltool1);
		ltools.add(ltool2);*/
		
		
	
		
	Collection<LearningTool> ltools =importLT("LearningTools.rdf", "RDF/XML");		
 

	
		rrw.saveResources(ltools);
		
	Collection<LearningTool>learningtools= rrw.loadResourcesFromDB(LearningTool.class);
		
	 try {
			JSONExporter jsonexp = new JSONExporterCls();
	System.out.println(jsonexp.convertLTToJSON(learningtools));

		
		}catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		
		
		
		
	

	}
	
	
	
	@SuppressWarnings("unchecked")
	public static Collection<LearningTool> importLT(String fileName, String syntax){
		RelsysResourceFactory rf = new RelsysResourceFactory();
		Model dataModel=null;
		try{
	    dataModel = ModelFactory.createDefaultModel();
		dataModel.setNsPrefix("lt","http://diplomski.nelakolundzija.org/LTontology.rdf#");
		dataModel.setNsPrefix("swc","http://data.semanticweb.org/ns/swc/ontology#");
		dataModel.setNsPrefix("jenabean", "http://thewebsemantic.com/");
		dataModel.setNsPrefix("dc", "http://purl.org/dc/elements/1.1/");
		dataModel.setNsPrefix("xsd", "http://www.w3.org/2001/XMLSchema#");
		dataModel.setNsPrefix("rdfs", "http://www.w3.org/2000/01/rdf-schema#");
		
		dataModel.read(new FileInputStream(fileName), "http://diplomski.nelakolundzija.org/LTontology.rdf#", syntax);
	} catch (Exception e) {
		logger.error(
						"Could not load DataModel from file: "+e);
	}
	
	
	RDF2Bean reader = new RDF2Bean(dataModel);
	 Collection<LearningTool> ltools = reader.loadDeep(rf.getBeanImplementationClass(LearningTool.class)); 
	  
	  LinkedList<LearningTool> list = (LinkedList<LearningTool>)ltools;

		Iterator<LearningTool> iter = list.iterator();

		while (iter.hasNext()) {
			LearningTool t = (LearningTool) iter.next();
	     System.out.println(t.getTitle());
		}
	 return ltools;

	  
	 
		
	}
	
	
	
	
	 
	 }


